<?php

session_start();

// include settings and functions
include "../conn/database.php";
include "functions.php";
include 'UUID.php';

// if the user submit the form
if(isset($_POST['submit'])) {
	// initialize errors values
	$errors=0;
	$errors_text = '';
	// cleanning the post data from hacking attempt
	$post_data = clean($_POST);
	
	// empty post data verification
	if(empty($_POST['addevent-step'])
	||empty($_POST['event_name']) || empty($_POST['event_titre'])
	|| empty($_POST['hh']) || empty($_POST['mn'])
	|| empty($_POST['adresse']) || empty($_POST['code'])
	|| empty($_POST['ville']) || empty($_POST['pays'])
	|| empty($_POST['voiture']) || empty($_POST['train']) || empty($_POST['avion'])) {
		$errors_text .= "Des champs sont vides";
		$errors++;
	}
		
	// time verification (is valid date)
	if(!check_time($post_data['hh'], $post_data['mn'])) {
		$errors_text .= "La date n'est pas valide";
		$errors++;
	}
	
	// if no errors we proceed
	if($errors == 0) {
		// data correction
		$wid = $_SESSION['wid'];
		$read_weddingdate = mysql_fetch_row(mysql_query("SELECT WEDDING_DATE FROM `T_WEDDINGS` WHERE WEDDING_ID ='".$wid."'"));
		$time = $read_weddingdate[0]." ".$post_data['hh'].":".$post_data['mn'];
		$index = $post_data['addevent-step'];
		
		// generating the event ID
		$eid = $_SESSION['eid-'.$post_data['addevent-step']];
		
		$insert_sql = "
		UPDATE `T_EVENTS` SET
		`START_TIME`='".$time."',`TITLE`='".$post_data['event_titre']."',`DATE`='".$read_weddingdate[0]."',`CAR_ACCESS_DESC`='".$post_data['voiture']."',
		`PLANE_ACCESS_DESC`='".$post_data['avion']."',`TRAIN_ACCESS_DESC`='".$post_data['train']."',`STREET`='".$post_data['adresse']."',
		`POSTAL_CODE`='".$post_data['code']."',`CITY`='".$post_data['ville']."',`COUNTRY`='".$post_data['pays']."',`EVENT_TYPE_ID`='".$post_data['event_name']."'
		WHERE `EVENT_ID`= '".$eid."'"
		;
		
		if(!mysql_query($insert_sql)) {
			$errors_text .= mysql_error()."<br />";
			$errors++;
		} else {
			$tagURL = "grandjour.php?event=".($post_data['addevent-step']);
		}
	}
	if($errors == 0) {
		$arr['success'] = "1"; 
        $arr['msg'] = 'Vos informations ont bien été enregistrées.';
	} else {
		$arr['success'] = "0"; 
        $arr['msg'] = $errors_text;
	}
	echo json_encode($arr);
}
?>